package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.nailcampus.acl.mapper.DynamicReplyMapper;
import com.hua.nailcampus.acl.mapper.UserMapper;
import com.hua.nailcampus.acl.model.DynamicReply;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.vo.DynamicReplyVo;
import com.hua.nailcampus.acl.model.vo.DynamicVo;
import com.hua.nailcampus.acl.service.DynamicReplyService;
import com.hua.nailcampus.constant.RedisConst;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author:chenjianhua
 * @Date:2023/12/23 23:26
 * @Version 17
 */
@Service
@Slf4j
public class DynamicReplyServiceImpl extends ServiceImpl<DynamicReplyMapper, DynamicReply>
    implements DynamicReplyService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserMapper userMapper;
    @Override
    public void sendComment(DynamicReply dynamicReply) {
        baseMapper.insert(dynamicReply);
        List<DynamicVo> dynamicVoList = (List<DynamicVo>) redisTemplate.opsForValue().get(RedisConst.DYNAMIC_LISTK+dynamicReply.getUserId().toString());
        for (DynamicVo dynamicVo : dynamicVoList){
            if(dynamicVo.getId().equals(dynamicReply.getDynamicId())){
                DynamicReplyVo dynamicReplyVo = new DynamicReplyVo();
                dynamicReplyVo.setContent(dynamicReply.getContent());
                User user = userMapper.selectById(dynamicReply.getUserId());
                dynamicReplyVo.setUserName(user.getUserName());
                dynamicVo.getReplyList().add(dynamicReplyVo);
            }
        }
        redisTemplate.opsForValue().set(RedisConst.DYNAMIC_LISTK+dynamicReply.getUserId().toString(),dynamicVoList,RedisConst.DYNAMIC_LIST_TIMEOUT, TimeUnit.MINUTES);
    }
}
